package cst.a729yt.startproject_729yt.model;

import cst.a729yt.startproject_729yt.bean.WeatherResult;
import cst.a729yt.startproject_729yt.bean.Weatherlnfo;
import cst.a729yt.startproject_729yt.iface.WeatherIface;
import cst.a729yt.startproject_729yt.listener.WeatherListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import cst.a729yt.startproject_729yt.service.WeatherService;

/**
 * Created by Administrator on 2017/5/7.
 */

public class WeatherModel implements WeatherIface {
    private Retrofit retrofit;
    private WeatherService service;
    public static final String BASE_URL="http://www.weather.com.cn";

    public WeatherModel() {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public Weatherlnfo getWeather(String c, final WeatherListener l) {
        service=retrofit.create(WeatherService.class);
        Call<WeatherResult>  call= service.getWeather(c);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                l.onResponse(response.body().getWeatherlnfo());
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                l.onFail(t.toString());

            }
        });
        return null;
    }
}
